<template>
  <a-cascader
    v-model="cascaderList"
    :options="adidList"
    :placeholder="placeholder"
    :allowClear="true"
    :showSearch="true"
    :changeOnSelect="true"
    @change="adidChange"
  />
</template>
<script>
export default {
  props: {
    //placeholder
    placeholder: {
      type: String,
      default: '请选择所属区域'
    },
    // 回显值
    value: {
      type: Array,
    },
    // 是否只能选最底级
    lowestLevel: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      adidList: [], // 搜索范围
      cascaderList: this.value || [], // 级联数据
    }
  },
  watch: {
    value(val) {
      this.cascaderList = val || []
    },
  },
  created() {
    // 过滤所属区域选择范围
    this.filterAdid()
  },
  methods: {
    // 过滤所属区域选择范围
    filterAdid() {
      let adidList = JSON.parse(JSON.stringify(this.$store.state.form.formData.dynamicDataList['ad4jx'][0].children))
      let countryMap = {}
      adidList.forEach((v) => {
        v.children.forEach((s) => {
          countryMap[s.value] = s.name
          delete s.children
        })
      })
      this.$emit('f_setCountryMap', countryMap)
      this.adidList = adidList
    },
    // 所属区域选中
    adidChange(value) {
      if(this.lowestLevel && value.length < 2) {
        this.cascaderList = []
        return
      }
      let country = []
      // 等于1说明选的是市级
      if (value.length == 1) {
        let city = value[0]
        // 找市级下的县
        for (let v of this.adidList) {
          if (v.value == city) {
            v.children.forEach((s) => {
              country.push(s.value)
            })
            break
          }
        }
      } else if (value.length == 2) {
        // 等于2说明选的是县级
        country.push(value[1])
      }
      this.$emit('f_adidChange', country, value)
    },
  },
}
</script>

<style lang="scss" scoped>
</style>
